//
//  SelectDateAndFlightVC.m
//  WestAir
//
//  Created by s on 16/9/22.
//  Copyright © 2016年 Henry. All rights reserved.
//

#import "SelectDateAndFlightVC.h"

@interface SelectDateAndFlightVC ()
@property (weak, nonatomic) IBOutlet UIButton *btnGoToHome;
@property(nonatomic,strong)NSMutableDictionary *dicMainData;

@property(nonatomic,strong)NSString  *currentDate;
@property(nonatomic,strong)NSString  *currentHangBan;
@property (weak, nonatomic) IBOutlet UIButton *btnSelectDate;
@property (weak, nonatomic) IBOutlet UIButton *btnSelectHangBan;

@end

@implementation SelectDateAndFlightVC
@synthesize dicMainData,viewSelectDate;

@synthesize btnGoToHome;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self  initDataAndUI];
    
    
    VIEW_CORNER(viewSelectDate, 4.0);
    
}

-(void)initDataAndUI{

    dicMainData=[NSMutableDictionary  new];
    self.view.backgroundColor=BG_GRAY_COLOR;
    
    VIEW_CORNER(btnGoToHome, buttonCorner21);
    btnGoToHome.backgroundColor=NORMAL_BULUE_COLOR;
    
    NSArray  *arrData=self.arrData;
    [arrData  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        NSDictionary  *dicdata=obj;
        NSString *strDate=dicdata[@"FFLIGHTDATE"];
        
        NSArray  *allKeys=[dicMainData  allKeys];
        
        __block  BOOL isHas=NO;
        [allKeys  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString  *strKey=obj;
            if ([strKey isEqualToString:strDate]) {
                isHas=YES;
                *stop=YES;
            };
        }];
//      如果存在
        if (isHas) {
            NSMutableArray  *arrhangBan=dicMainData[strDate];
            [arrhangBan  addObject:dicdata[@"FFLIGHTNUMBER"]];
            [dicMainData  setObject:arrhangBan forKey:strDate];
        }
        else{
            
            NSMutableArray  *arrHangBan=[NSMutableArray  new];
            [arrHangBan  addObject:dicdata[@"FFLIGHTNUMBER"]];
             [dicMainData  setObject:arrHangBan forKey:strDate];
        }
        ;
    }];
    
    
}


- (IBAction)btnSelectDate:(id)sender {
    
    NSArray  *arrAddressKey=[dicMainData  allKeys];
    NSMutableArray  *arrAction=[NSMutableArray new];
    
    for ( NSInteger i =0; i<arrAddressKey.count; i++) {
        
        NSString     *strDate=arrAddressKey[i];
        
        CMCCIOTIGAlertActionCustomer  *actionCustomer=  [CMCCIOTIGAlertActionCustomer  actionCustomerWithTitle:strDate style:UIAlertActionCustomeStyleDarkGray  handler:^(CMCCIOTIGAlertActionCustomer * _Nullable action) {
            
           
            if ([_currentDate isEqualToString:action.title]) {
                return ;
            }
            _currentDate= action.title;
            [_btnSelectDate  setTitle:_currentDate forState:UIControlStateNormal];
            _currentHangBan=nil;
           
            [self.btnSelectHangBan  setTitle:@"请选择航班" forState:UIControlStateNormal];
        }];

       BOOL  isSelect=[_currentDate  isEqual:strDate]?YES:NO;
       isSelect?[actionCustomer setIsSelect:YES]:nil;
       [arrAction addObject:actionCustomer];
        
    }
    
    [CMCCIOTTipsCenter   showAlertAddressTitle:@"选择日期" message:@"" arrAction:arrAction];
    
}
- (IBAction)selectHangBan:(id)sender {
    
    if (_currentDate==nil) {
        
        [CMCCIOTTipsCenter  showErrorWithStatus:@"日期不能为空"];
         return ;
    }
    
    NSArray  *arrAddressKey=dicMainData[_currentDate];
    NSMutableArray  *arrAction=[NSMutableArray new];
    
    for ( NSInteger i =0; i<arrAddressKey.count; i++) {
        
        NSString     *strHangBan=arrAddressKey[i];
        
        CMCCIOTIGAlertActionCustomer  *actionCustomer=  [CMCCIOTIGAlertActionCustomer  actionCustomerWithTitle:strHangBan style:UIAlertActionCustomeStyleDarkGray  handler:^(CMCCIOTIGAlertActionCustomer * _Nullable action) {
            
            _currentHangBan= action.title;

            [_btnSelectHangBan  setTitle:_currentHangBan forState:UIControlStateNormal];
            
        }];
        
        BOOL  isSelect=[_currentHangBan  isEqual:strHangBan]?YES:NO;
        isSelect?[actionCustomer setIsSelect:YES]:nil;
        [arrAction addObject:actionCustomer];
        
    }
    
    [CMCCIOTTipsCenter   showAlertAddressTitle:@"选择航班" message:@"" arrAction:arrAction];
    
}


- (IBAction)btnSelectPushHome:(id)sender {
    
   
    if (_currentDate.length&&_currentHangBan.length) {
        
        NSDictionary  *dicParama=@{@"FNUMBER":_currentHangBan,@"FFLIGHTDATE":_currentDate};
        NSDictionary  *dicJson=  [CMCCIOTIGUnity   getRequestJson:dicParama];
        [[NetworkFactory  alloc]  startPostRequestWithURL:FLIGHTMENBERLIST andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
            
            if ([responseJson[@"IsSuccess"]  integerValue]==1) {
                
                
                NSArray  *arrData;
                if ([responseJson[@"data"] isKindOfClass:[NSArray  class]]) {
                    arrData=responseJson[@"data"];
                    
                }
                else{
                    arrData=@[];
                 
                }
                
               if(arrData.count){
                   NSDictionary  *dicData=arrData[0];
                   HangBanInfo   *hangBan=[HangBanInfo  currentHangBan];
                   hangBan.FFLIGHTDATE=[(NSString  *)dicData[@"FFLIGHTDATE"]  nullClass];
                   hangBan.FFLIGHTNUMBER=_currentHangBan;
                   hangBan.FID=[dicData[@"FID"]  integerValue];
                   hangBan.FIsAuthorize=[ (NSString  *)dicData[@"FIsAuthorize"]  nullClass];
                   hangBan.FIsInventory=[(NSString  *)dicData[@"FIsInventory"]  nullClass];
                   hangBan.FIsInventoryHS=[(NSString  *)dicData[@"FIsInventoryHS"]  nullClass];
                   
                   NSMutableArray  *arrMeber=[[NSMutableArray  alloc]  init];
                   
                   NSArray  *arrMemberParma;
                   
                   if([dicData[@"memberList"]  isKindOfClass:[NSArray  class]]){
                   
                       arrMemberParma=dicData[@"memberList"];
                   }
                   else{
                   
                       arrMemberParma=@[];
                   }
                   
                   
                  
                   if (arrMemberParma.count) {
                        BOOL  isHasMember=NO;
                       for (int i=0; i<arrMemberParma.count; i++) {
                           MemberList  *member=[[MemberList  alloc]  init];
                           NSDictionary  *dicMember=arrMemberParma[i];
                           member.isSaleHost=[(NSString  *)dicMember[@"isSaleHost"]  nullClass];
                          member.userAccount=[(NSString  *)dicMember[@"userAccount"]  nullClass];
                           member.FEntryID=[STRING_FORMAT(dicMember[@"FEntryID"])  integerValue ]  ;
                           member.post=[dicMember[@"post"]  nullClass];
                          
                          member.userID=[dicMember[@"userID"]  integerValue];
                           
                         
                           
                           member.userName=[(NSString  *)dicMember[@"userName"]  nullClass];
                            member.isSaleHost=[(NSString  *)dicMember[@"isSaleHost"]  nullClass];
                            [arrMeber addObject:member];
                           
                           MemberList  *memberShare=[MemberList  currentUsinfo];
                           NSString  *userAccount=memberShare.userAccount;
                           NSString  *memberAccount=member.userAccount ;
                           if ([userAccount isEqualToString:memberAccount]) {
                            
                             memberShare.userName=member.userName;
                              memberShare.userID=member.userID;
                               isHasMember=YES;
                               
                           }
                        
                         
                       }
                       
                       hangBan.arrMember=arrMeber;
                       
                       if (isHasMember) {
                           [self performSegueWithIdentifier:@"toHomeVC" sender:nil];
                       }
                       else{
                           
                           [CMCCIOTTipsCenter  showErrorWithStatus:@"当前机组人员不存在该登录的用户"];
                           [self performSelector:@selector(popBack) withObject:nil afterDelay:1.0];
                       }

                      
                   }
                   
                   
                }
               else{
               
               
                   [CMCCIOTTipsCenter  showErrorWithStatus:@"暂无此航班信息"];
                   return ;
               }
                   
            
            }
            
        } andFailBlock:^(NSError *error) {
            
                        
        } ];

    }
    else{
    
        [CMCCIOTTipsCenter  showErrorWithStatus:@"请选择航班日期和航班号!"];
    
    }

    
    
}


-(void)popBack{

    [self.navigationController  popViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
